package com.qcxk.yuqian.system.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcxk.yuqian.system.entity.Role;
import com.qcxk.yuqian.system.entity.RoleAuth;
import com.qcxk.yuqian.system.entity.UserRole;
import com.qcxk.yuqian.system.mapper.RoleAuthMapper;
import com.qcxk.yuqian.system.mapper.RoleMapper;
import com.qcxk.yuqian.system.mapper.UserRoleMapper;
import com.qcxk.yuqian.system.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImp extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleAuthMapper roleAuthMapper;

    @Override
    public Page queryRoleList(Page page, Role role) {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(role.getName())) {
            roleQueryWrapper.like("name", role.getName());
        }
        roleMapper.selectPage(page, roleQueryWrapper);
        return page;
    }

    @Override
    public boolean updateRole(Role role) {
        Role item = roleMapper.selectById(role.getId());
        if (StringUtils.isNotBlank(role.getName())) {
            item.setName(role.getName());
        }
        if (StringUtils.isNotBlank(role.getDescription())) {
            item.setDescription(role.getDescription());
        }
        if (StringUtils.isNotBlank(role.getDataAccess())) {
            item.setDataAccess(role.getDataAccess());
        }
        int re = roleMapper.updateById(item);
        return re == 1 ? true : false;
    }

    @Override
    public boolean removeRole(Integer id) {
        int re = roleMapper.deleteById(id);
        if (re == 1) {
            QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
            userRoleQueryWrapper.eq("role_id", id);
            userRoleMapper.delete(userRoleQueryWrapper);
            QueryWrapper<RoleAuth> roleAuthQueryWrapper = new QueryWrapper<>();
            roleAuthQueryWrapper.eq("role_id", id);
            roleAuthMapper.delete(roleAuthQueryWrapper);
            return true;
        }
        return false;
    }

    @Override
    public boolean grantAuthToRole(Integer roleId, Integer[] idarr) {
        RoleAuth roleAuth;
        if (roleId != null && roleId.intValue() != 0) {
            QueryWrapper<RoleAuth> roleAuthQueryWrapper = new QueryWrapper<>();
            roleAuthQueryWrapper.eq("role_id", roleId);
            roleAuthMapper.delete(roleAuthQueryWrapper);
            try {
                for (Integer item : idarr) {
                    roleAuth = new RoleAuth();
                    roleAuth.setAuthId(item);
                    roleAuth.setRoleId(roleId);
                    roleAuthMapper.insert(roleAuth);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    @Override
    public boolean grantRoleForUser(Integer userId, Integer roleId) {
        UserRole userRole = new UserRole();
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id", userId);
        userRoleMapper.delete(userRoleQueryWrapper);
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        int re = userRoleMapper.insert(userRole);
        return re == 1 ? true : false;
    }
}
